package 单周赛.history;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class 第289场单周赛 {

    public static void main(String[] args) {

        System.out.println(digitSum("1", 2));

        int[] tasks = {2, 2, 3, 3, 2, 4, 4, 4, 4, 4};
        System.out.println(minimumRounds(tasks));

    }

    /**
     * 6070. 计算字符串的数字和
     * 模拟
     *
     * @param s
     * @param k
     * @return
     */
    public static String digitSum(String s, int k) {
        StringBuilder stringBuilder = new StringBuilder(s);
        while (s.length() > k) {
            stringBuilder = new StringBuilder();
            for (int i = 0; i < s.length(); i += k) {
                int curSum = 0;
                for (int j = 0; j < k && i + j < s.length(); j++) {
                    curSum += s.charAt(i + j) - '0';
                }
                stringBuilder.append(curSum);
            }
            s = stringBuilder.toString();
        }
        return stringBuilder.toString();
    }

    /**
     * 6071. 完成所有任务需要的最少轮数
     *
     * @param tasks
     * @return
     */
    public static int minimumRounds(int[] tasks) {
        int ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int task : tasks) {
            map.put(task, map.getOrDefault(task, 0) + 1);
        }
        for (Integer task : map.keySet()) {
            Integer count = map.get(task);
            if (count == 1) {
                return -1;
            }
            if (count % 3 == 0) {
                ans += count / 3;
            } else {  // 剩下1，最后4个，2 2 完成，剩下2个，3 2 完成多一轮
                ans += count / 3 + 1;
            }
        }
        return ans;
    }

}
